# -*- coding: utf-8 -*-
import re

import time
from sqlalchemy import create_engine, Column,String,Integer
from sqlalchemy.dialects.mysql import MEDIUMTEXT
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import pymysql
from bs4 import BeautifulSoup as bs
from dingdian.models import VulExploit
from dingdian.init_mysql import session
Base = declarative_base()

import sys
reload(sys)
sys.setdefaultencoding('utf-8')


pattern = re.compile(r'\d{4}-\d+')


rows = session.query(VulExploit).count()
for row in range(1,rows+1):

        print row
        vul = session.query(VulExploit).filter(VulExploit.id == row).all()
        #print row
        try:
          cveId =vul[0].cve_id
        except:
          print '************************error',row
          continue
        if cveId is not None:
          print "before:",cveId
          cveId = 'CVE-'+re.search(pattern, cveId).group()

          session.query(VulExploit).filter(VulExploit.id == row).update({VulExploit.cve_id:cveId})
          print "after:", session.query(VulExploit).filter(VulExploit.id == row).one().cve_id
        #session.add(new_vul)
        session.commit()


